if (localStorage.getItem("id") && localStorage.getItem("token")) {
    $.ajax({
        url: "http://localhost:8888/users/info",
        method: "get",
        data: {
            id: localStorage.getItem("id")
        },
        headers: {
            authorization: localStorage.getItem("token")
        },
        success: function (res) {
            console.log(res);
            yonghu(res.info.nickname)
            tuichu()
        }
    })

} else {
    console.log("没有");
}
function yonghu(a) {
    $(".header-left").html(`
    <span>您好! ${a} </span>
    <a href="login.html">退出</a>

    `)
}
function tuichu() {
    $(".header-left>a").on("click", function () {
        if (confirm("是否确定退出")) {
            $.ajax({
                url: "http://localhost:8888/users/logout",
                method: "get",
                headers: {
                    authorization: localStorage.getItem("token")
                },
                data: {
                    id: localStorage.getItem("id")
                },
                success: function (res) {
                    console.log(res);
                    alert(res.message)
                    location.href = "home.html"
                    localStorage.removeItem("id")
                    localStorage.removeItem("token")
                }
            })
        }
        return false
    })
}
$(".geren>p:nth-of-type(4)").on("click", function () {
    if (localStorage.getItem("id") && localStorage.getItem("token")) {
        location.href = `account.html`
    } else {
        alert("请先登录")
    }
    return false
})
$.ajax({
    url: "http://localhost:8888/goods/category",
    method: "get",
    success: function (res) {
        console.log(res);
        const arr = res.list
        for (var i = 1; i < 13; i++) {
            $(`<li>${arr[i]}</li>`)
            $(".left>ul").append($(`<li>${arr[i]}<b>&gt;</b></li>`))
        }
        $(".left>ul>li").on('click', function () {
            let str = $(this)[0].innerText.slice(0, $(this)[0].innerText.length - 2)
            console.log(str);
            localStorage.setItem("category", str)
            localStorage.removeItem("value")
            location.href = "list.html"
        })
    }
})
$("body>.box nav div .right li>a").on("click", function (e) {
    const Text = e.target.innerText
    if (Text === "首页") {
        location.href = "home.html"
    } else if (Text === "海外职购") {
        localStorage.removeItem("value")
        localStorage.removeItem("category")
        localStorage.removeItem("gods_id")
        location.href = "list.html"
    } else {
        $(this).attr("class", "active").parent().siblings().children().removeAttr("class")

    }
    return false
})
// ____
$.ajax({
    url: "http://localhost:8888/goods/item",
    data: {
        id: localStorage.getItem("gods_id"),
    },
    success: function (res) {
        console.log(res);

        img(res.info.img_big_logo)
        te(res.info.title)
        jia(res.info.price, res.info.current_price)
        box2(res.info.goods_introduce)
    }
})
function img(src) {
    $("body>section>.box>.img img").attr("src", src)
}
function te(a) {
    $("body>section>.box>.text span").text(a)
}
function jia(a, b) {
    $("body>section>.box>.jia del").text(a)
    $("body>section>.box>.jia i").text(b)
}
function box2(a) {
    $("body>section>.box2").html(a)
}
// 放大镜部分 
setTimeout(function () {
    $("body>section>.box>.img img").on("mouseover", move)
}, 100)
function move() {
    $("body>section>.box>.img>.zhezhao").show()
    $("body>section>.box>.fangda").show()
    $("body>section>.box>.fangda img").attr("src", $("body>section>.box>.img img").attr("src"))
    $("body").on("mousemove", function (e) {
        var x = e.pageX
        var y = e.pageY
        var l = x - $("body>section>.box>.img>.zhezhao").width() / 2
        var t = y - $("body>section>.box>.img>.zhezhao").height() / 2
        if (l < $("body>section>.box>.img img").offset().left) {
            l = $("body>section>.box>.img img").offset().left
        }
        if (t < $("body>section>.box>.img img").offset().top) {
            t = $("body>section>.box>.img img").offset().top
        }
        console.log();
        if (l > $("body>section>.box>.img img").offset().left + $("body>section>.box>.img img").outerWidth() - $("body>section>.box>.img>.zhezhao").width()) {
            l = $("body>section>.box>.img img").offset().left + $("body>section>.box>.img img").outerWidth() - $("body>section>.box>.img>.zhezhao").width()
        }
        if (t > $("body>section>.box>.img img").offset().top + $("body>section>.box>.img img").outerHeight() - $("body>section>.box>.img>.zhezhao").height()) {
            t = $("body>section>.box>.img img").offset().top + $("body>section>.box>.img img").outerHeight() - $("body>section>.box>.img>.zhezhao").height()
        }
        var L = ($("body>section>.box>.img>.zhezhao").position().left / $("body>section>.box>.img img").width()) * 800
        var T = ($("body>section>.box>.img>.zhezhao").position().top / $("body>section>.box>.img img").height()) * 800
        $("body>section>.box>.img>.zhezhao").offset({
            left: l,
            top: t,
        })
        $("body>section>.box>.fangda img").css({
            left: -L + "px",
            top: -T + "px",
        })
    })
}
$("body>section>.box>.img>.zhezhao").on("mouseout", function () {
    $("body>section>.box>.img>.zhezhao").hide()
    $("body>section>.box>.fangda").hide()
    $("body").off("mousemove")
})
// 看了又看
$.ajax({
    url: "http://localhost:8888/goods/list",
    method: "get",
    data: {
        saleType: Math.floor(Math.random() * (5 - 8) + 8),
    },
    success: function (res) {

        Look(res.list)
    }
})
function Look(list) {
    for (let i = 0; i < list.length; i++) {
        $("body>section>.box3")[0].innerHTML += `
        <div index=${list[i].goods_id}>
            <img src="${list[i].img_big_logo}" alt="">
            <p>${list[i].title}</p>
            <b>￥${list[i].current_price}</b>
        </div>
        `
    }
    $("body>section>.box3>div").on("click", function () {
        localStorage.setItem("gods_id", $(this).attr("index"))
        location.href = "data.html"
    })
}
// 相关推荐
$.ajax({
    url: "http://localhost:8888/goods/list",
    method: "get",
    data: {
        current: Math.floor(Math.random() * (6 - 8) + 8),
    },
    success: function (res) {
        console.log(res);
        tuijian(res.list)
    }
})
function tuijian(list) {
    if (list.length > 6) {
        list.length = 6
    }
    for (let i = 0; i < list.length; i++) {
        $("body>section>.tui>section")[0].innerHTML += `
        <div index=${list[i].goods_id}>
            <img src="${list[i].img_big_logo}" alt="">
            <p>${list[i].title}</p>
            <b>￥${list[i].current_price}</b>
        </div>
        `

    }
    $("body>section>.tui>section>div").on("click", function () {
        localStorage.setItem("gods_id", $(this).attr("index"))
        location.href = "data.html"
    })
}
// 加入购物车
// +
$("body>section>.box>.go>p button:nth-of-type(1)").on("click", function () {
    $("body>section>.box>.go>p input")[0].value = +$("body>section>.box>.go>p input").attr("value") + 1
    $("body>section>.box>.go>p input").attr("value", $("body>section>.box>.go>p input")[0].value)
})
// —
$("body>section>.box>.go>p button:nth-of-type(2)").on("click", function () {
    $("body>section>.box>.go>p input")[0].value = +$("body>section>.box>.go>p input").attr("value") - 1
    if ($("body>section>.box>.go>p input")[0].value < 0) {
        $("body>section>.box>.go>p input")[0].value = 0
    }
    $("body>section>.box>.go>p input").attr("value", $("body>section>.box>.go>p input")[0].value)
})
// 数字改变
$("body>section>.box>.go>p input").on("input", function () {
    $(this).attr("value", $(this)[0].value)
})
// 获取焦点
$("body>section>.box>.go>p input").on("focus", function () {
    console.log(123);
    $(this).attr("value", ` `)
    $("body>section>.box>.go>p input")[0].value = ` `
})
// 添加购物车
$("body>section>.box>.go>button:nth-of-type(2)").on("click", function () {
    const num = +$("body>section>.box>.go>p input").attr("value")
    if (!(localStorage.getItem("id") && localStorage.getItem("token"))) {
        alert("请先登录")
        return
    }
    for (var i = 0; i < num; i++) {
        $.ajax({
            url: "http://localhost:8888/cart/add",
            data: {
                id: localStorage.getItem("id"),
                goodsId: localStorage.getItem("gods_id"),
            },
            method: "post",
            headers: {
                authorization: localStorage.getItem("token"),
            },
            success: function (res) {
                console.log(res);
                if (!localStorage.getItem("id")) {
                    alert("请先登录")
                }
            }
        })
    }
    if (num !== 0) {
        alert(`加入购物车成功，已加入购物车此商品${num}个`)
    } else {
        alert("加入购物车的商品数量不能为0")
    }
    $("body>section>.box>.go>p input").attr("value", 1)
    $("body>section>.box>.go>p input")[0].value = 1
})
// 直接购买
$("body>section>.box>.go>button:nth-of-type(1)").on("click", function () {
    console.log()
    if (localStorage.getItem("id") && localStorage.getItem("token")) {
        $("body header").before($(`<aside>
    <b>向考拉海购支付</b>
    <span>X</span>
    <p>共<b>${(+$("body>section>.box>.go>p>input").attr("value"))  * Number(($("body>section>.box>.jia span:nth-of-type(2) i")[0].innerText))}</b>元</p>
    <button>暂时放弃</button>
    <button>确认支付</button>
    </aside>`))
        $("body>aside button:nth-of-type(1)").on("click", function () {
            $("aside").remove()
        })
        $("body>aside button:nth-of-type(2)").on("click", function () {
            $("aside").remove()
            alert("支付成功！")
        })
        $("body>aside span").on("click", function () {
            $("aside").remove()
        })
    } else {
        alert("请先登录")
    }
})
// 搜索内容
$("nav section .search .box div input").on("input", function () {
    $("nav section .search span").on("click", function () {
        let value = $("nav section .search .box div input")[0].value
        localStorage.setItem("value", value)
        console.log(value);
        setTimeout(function () {
            $("nav section .search .box div input")[0].value = ``
        }, 500)
        location.href = "list.html"
    })
    $("body").on("keydown", function (e) {
        if (e.keyCode === 13) {
            let value = $("nav section .search .box div input")[0].value
            localStorage.setItem("value", value)
            setTimeout(function () {
                $("nav section .search .box div input")[0].value = ``
            }, 500)
            localStorage.removeItem("category")
            location.href = "list.html"
        }
    })
})